package com.vastcom.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.result.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;
@ResultPath
public class JsonResult extends StrutsResultSupport {
	private Object result;  
    private JsonConfig jsonConfig;  
  
    public Object getResult() {  
        return result;  
    }  
  
    public JsonResult(JsonConfig jsonConfig) {  
        super();  
        this.jsonConfig = jsonConfig;  
    }  
    
    
  
    public void setResult(Object result) {  
        this.result = result;  
    }  
  
/*    public json(JsonConfig jsonConfig) {
		super();
		this.jsonConfig = jsonConfig;
	}*/

	private static final long serialVersionUID = 7978145882434289002L;  
  
    @Override  
    protected void doExecute(String finalLocation, ActionInvocation invocation)  
            throws Exception {  
        HttpServletResponse response = null;  
        try {  
            response = ServletActionContext.getResponse();  
            PrintWriter printWriter = response.getWriter();  
            if (jsonConfig != null) {  
                printWriter.write(JSONObject.fromObject(result).toString());  
            } else {  
                printWriter.write(JSONObject.fromObject(result, jsonConfig)  
                        .toString());  
            }  
        }catch(Exception e){  
            throw new Exception("json parse error!");  
        } finally {  
            response.getWriter().close();  
        }  
  
          
  
    }  
}
